﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.Common;
using EShop.Model;
using EShop.BLL;

namespace EShop.Client
{
    public partial class frmEmployeeReg : frmBase
    {
        /// <summary>
        /// reg new user.
        /// </summary>
        public frmEmployeeReg()
        {
            InitializeComponent();
        }

        private void frmUserReg_Load(object sender, EventArgs e)
        {

        }

        private void btnReset_Click(object sender, EventArgs e)
        {//重置
            this.txtFullName.Clear();
            this.cbbSex.SelectedIndex = -1;
            this.txtPID.Clear();
            //this.txtBirthday.Clear();
            dtBrithday.Value = DateTime.Now.AddYears(-1);
            this.cbxIsGregorianCalendar.Checked = false;
            this.txtPopedom.Clear();
            this.txtAddress.Clear();
            this.txtMobile.Clear();
            this.rtbRemark.Clear();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string strFullName = SystemLibrary.SQLQuoteName(txtFullName.Text.Trim());
            int iSex = cbbSex.SelectedIndex;
            string strPID = SystemLibrary.SQLQuoteName(txtPID.Text.Trim());
            string strBirthday = SystemLibrary.SQLQuoteName(dtBrithday.Value.ToString("yyyy-MM-dd"));
            int iIsGregorianCalendar = Convert.ToInt32(cbxIsGregorianCalendar.Checked);
            string strPopedom = SystemLibrary.SQLQuoteName(txtPopedom.Text.Trim());
            string strAddress = SystemLibrary.SQLQuoteName(txtAddress.Text.Trim());
            string strMobile = SystemLibrary.SQLQuoteName(txtMobile.Text.Trim());
            string strRemark = SystemLibrary.SQLQuoteName(rtbRemark.Text);

            #region 验证输入信息
            if (string.IsNullOrEmpty(strFullName))
            {
                MessageBox.Show("请输入员工姓名！");
                return;
            }
            if (iSex==-1)
            {
                MessageBox.Show("请选择员工性别！");
                return;
            }
            if (string.IsNullOrEmpty(strPID))
            {
                MessageBox.Show("请输入员工身份证号码！");
                return;
            }
            if (string.IsNullOrEmpty(strBirthday))
            {
                MessageBox.Show("请输入员工生日！");
                return;
            }
            try
            {
                Convert.ToDateTime(strBirthday);
            }
            catch { return; }
            if (string.IsNullOrEmpty(strPopedom))
            {
                MessageBox.Show("请输入员工籍贯！");
                return;
            }
            if (string.IsNullOrEmpty(strAddress))
            {
                MessageBox.Show("请输入员工联系地址！");
                return;
            }
            #endregion

            EShop.Model.EmployeeInfo employeeInfo = new EShop.Model.EmployeeInfo();
            employeeInfo.FullName = strFullName;
            employeeInfo.Sex = iSex==1?true:false;
            employeeInfo.EmployeePID = strPID;
            employeeInfo.Brithday = Convert.ToDateTime(strBirthday); ;
            employeeInfo.IsGregorianCalendar = iIsGregorianCalendar == 1 ? true : false;
            employeeInfo.Popedom = strPopedom;
            employeeInfo.Address = strAddress;
            employeeInfo.Mobile = strMobile;
            employeeInfo.Remark = strRemark;
            EShop.BLL.EmployeeBLL oEmployeBLL = new EShop.BLL.EmployeeBLL();
            DataTable tableNewEmployeeInfo = oEmployeBLL.AppendEmployeeInfo(employeeInfo);
            if (tableNewEmployeeInfo.Rows.Count<=0)
            {
                MessageBox.Show("新员工添加失败，失败的原因可能是该身份证号码已经注册过了。");
                this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                //EmployeeID,EmployeePWD,FullName,EmployeePID,Sex,Brithday,IsGregorianCalendar,Popedom,[Address],Mobile,Remark,CreateDatetime,EmployeePurview 
                string msg = string.Empty;
                msg += "新员工添加成功，信息如下：";
                msg += "\r\n\t新员工工号:\t" + tableNewEmployeeInfo.Rows[0]["EmployeeID"].ToString();
                msg += "\r\n\t新员工密码:\t" + tableNewEmployeeInfo.Rows[0]["EmployeePWD"].ToString();
                msg += "\r\n\t新员工姓名:\t" + tableNewEmployeeInfo.Rows[0]["FullName"].ToString();
                msg += "\r\n\t身份证号码:\t" + tableNewEmployeeInfo.Rows[0]["EmployeePID"].ToString();
                msg += "\r\n\t新员工性别:\t" + ((bool)tableNewEmployeeInfo.Rows[0]["Sex"] == false ? "男" : "女");
                string s=
                msg += "\r\n\t新员工生日:\t" + ((bool)tableNewEmployeeInfo.Rows[0]["IsGregorianCalendar"] == false ? "农历" : "公历") + ":" + tableNewEmployeeInfo.Rows[0]["Brithday"].ToString();
                msg += "\r\n\t新员工籍贯:\t" + tableNewEmployeeInfo.Rows[0]["Popedom"].ToString();
                msg += "\r\n\t新员工联系地址:\t" + tableNewEmployeeInfo.Rows[0]["Address"].ToString();
                msg += "\r\n\t新员工手机号码:\t" + tableNewEmployeeInfo.Rows[0]["Mobile"].ToString();
                msg += "\r\n\t新员工备注:\t" + tableNewEmployeeInfo.Rows[0]["Remark"].ToString();
                msg += "\r\n\t新员工添加时间:\t" + tableNewEmployeeInfo.Rows[0]["CreateDatetime"].ToString();
                msg += "\r\n\t新员工权限:\t" + tableNewEmployeeInfo.Rows[0]["EmployeePurview"].ToString();
                MessageBox.Show(msg);
                this.DialogResult = DialogResult.OK;
            }
            Close();
        }
    }
}
